package de.westranger.forex.code.script;

import static org.junit.Assert.*;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;

import org.junit.Test;

public class MQLCompareImplementations {

	@Test
	public void testCompareIndicatorImplementattion() throws FileNotFoundException, IOException{
		final String resultMQL = "E:\\EUR_USD_2012\\mql_validation_mql_indicator.csv.gz";
		final String resultJava = "E:\\EUR_USD_2012\\mql_validation_java_indicator.csv.gz";
		final BufferedReader bisMQL = new BufferedReader(new InputStreamReader(new GZIPInputStream(new BufferedInputStream(new FileInputStream(resultMQL)))));
		final BufferedReader bisJava = new BufferedReader(new InputStreamReader(new GZIPInputStream(new BufferedInputStream(new FileInputStream(resultJava)))));
		boolean firstLine = false;
		
		List<String> rawMQL = new ArrayList<String>();
		List<String> rawJava = new ArrayList<String>();
		
		while(bisMQL.ready()){
			final StringTokenizer stMQL = new StringTokenizer(bisMQL.readLine(),";");
			final StringTokenizer stJava = new StringTokenizer(bisJava.readLine(),";");

			while(stMQL.hasMoreElements()){
				rawMQL.add(stMQL.nextToken());
				rawJava.add(stJava.nextToken());
			}
			assertEquals(rawMQL.size(),rawJava.size());
			
			if(!firstLine){
				firstLine = true;
			}else{
				for(int i=0;i<rawMQL.size();i++){
					//System.out.println(Double.parseDouble(rawMQL.get(i)) + " == " +Double.parseDouble(rawJava.get(i)));
					assertEquals(Double.parseDouble(rawMQL.get(i)),Double.parseDouble(rawJava.get(i)),0.00000005);
				}
			}

			rawMQL.clear();
			rawJava.clear();
			assertTrue(stMQL.hasMoreTokens() == stJava.hasMoreTokens());
		}

		assertTrue(bisMQL.ready() == bisJava.ready());
		bisMQL.close();
		bisJava.close();
	}

	@Test
	public void testCompareRuleImplementation(){
		fail("not yet implemented -- will also be a file comparison with 2 boolean columns");
	}
}
